<template>
  <div class="create">
    <h2>{{ $route.params.id ? "编辑" : "创建" }}用户</h2>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="旧密码" v-if="$route.params.id">
        <el-input v-model="form.passwordOld"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{
          $route.params.id ? "修改保存" : "立即创建"
        }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        id: "",
        username: "",
        password: null,
        passwordOld: "",
      },
    };
  },
  methods: {
    async onSubmit() {
      console.log("submit!");
      let url =
        this.form.id == "" ? "common/admin_users" : "common/admin_users/edit";
      const res = await this.$http.post(url, this.form);
      console.log(res);

      this.$message({
        type: "success",
        message: this.form.id == "" ? "创建成功!" : "修改成功",
      });

      this.form = {
        id: "",
        name: "",
        parent: null,
      };
    },
    async getItem(id) {
      const res = await this.$http.get(`common/admin_users/${id}`);
      this.form = res.data;
      this.form.id = id;
    },
  },
  created() {
    this.$route.params.id && this.getItem(this.$route.params.id);
  },
  mounted() {},
};
</script>